<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(BASEPATH.'../fuman/ebay/keys.php');
require_once(BASEPATH.'../fuman/ebay/eBaySession.php');


/**
* ebay 反馈物流信息
*/
class Ebayreturn
{
	
	function __construct()
	{
				 $this->CI =& get_instance();
        $this->db1 = $this->CI->load->database('default', TRUE);
	}


public function returne(){
  $row = $this->db1->get_where("d_shop","platfromname='ebay' AND  amazonkey !='' and agent_visit = 0")->result_array();
  $serverUrl='https://api.ebay.com/ws/api.dll';
$verb = 'CompleteSale';
  foreach ($row as $v) {
    $shopid=$v['id'];
  	$shopname=$v['name'];
    $data=json_decode($v['amazonkey'],true);
    if (!isset($data['ebaydevid'])) {
      continue;
    }
    $devID=$data['ebaydevid'];
    $userToken=$data['ebayusertoken'];
    $appID=$data['ebayappid'];
    $certID=$data['ebaycertid'];
    $siteID=$data['ebaysiteid'];
  $compatabilityLevel=$data['ebaycompatabilitylevel'];
//$compatabilityLevel=967;
$listrow = $this->db1->get_where("db_order_list","shopname='{$shopname}' and (order_status='4' or order_status='5') and is_update_platform='0'")->result_array();

if(count($listrow)==0)
{
	continue;
}

foreach ($listrow as $value) {
	$order_id=$value['order_id'];
$shipping_number=$value['shipping_number'];
$iflk=substr($shipping_number,0,2);

/*
if ($iflk=='LK') {
	$this->db1->update("db_order_list",array("is_update_platform"=>'1'),"order_id=".$order_id);
  continue;
}
*/


$shipping_id=$value['shipping_id'];
$tradeid=$value['tradeid'];
//$ebay_orderID=$value['ebay_orderID'];
$TransactionID=$value['order_sn'];
$ItemID=substr($value['ebay_orderID'],0,strpos($value['ebay_orderID'],'-'));
$shippingrow=$this->db1->get_where("db_order_express","id='{$shipping_id}'")->result_array();

foreach ($shippingrow as $sv) {
  $shipping_name=$sv['ebay_express'];
}


if($value['shipping_time']==0)
{
	$value['shipping_time'] = time();
}
$shipping_time=gmdate("Y-m-d\TH:i:s",$value['shipping_time']);

$requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
$requestXmlBody .= '<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
//$requestXmlBody .="<ListingType>FixedPriceItem</ListingType>";

// $requestXmlBody .= '<WarningLevel>High</WarningLevel>';
//$requestXmlBody .= '<Paid>true</Paid>';
$requestXmlBody .= "<Version>{$compatabilityLevel}</Version><ItemID>{$ItemID}</ItemID>";
$requestXmlBody .= "<Shipped>true</Shipped>";
//$requestXmlBody .= "<OrderID>{$ebay_orderID}</OrderID>";
$requestXmlBody .="<Shipment>";
$requestXmlBody .="<ShipmentTrackingDetails>";
$requestXmlBody .="<ShipmentTrackingNumber>{$shipping_number}</ShipmentTrackingNumber>";
$requestXmlBody .="<ShippingCarrierUsed>{$shipping_name}</ShippingCarrierUsed>";
$requestXmlBody .="</ShipmentTrackingDetails>";
$requestXmlBody .="<ShippedTime>{$shipping_time}</ShippedTime>";
$requestXmlBody .="</Shipment>";

$requestXmlBody .= "<TransactionID>{$TransactionID}</TransactionID>";
$requestXmlBody .= "<RequesterCredentials><eBayAuthToken>$userToken</eBayAuthToken></RequesterCredentials>";
//$requestXmlBody .= "<Version>{$compatabilityLevel}</Version>";
$requestXmlBody .= '</CompleteSaleRequest>';

$session = new eBaySession($userToken, $devID, $appID, $certID, $serverUrl, $compatabilityLevel, $siteID, $verb);
$responseXml = $session->sendHttpRequest($requestXmlBody);


$xmlst=simplexml_load_string($responseXml);
			
$data = json_decode(json_encode($xmlst),TRUE);
if($data['Ack'] =='Success')
{
		$imgdata="";
		$imgdata["goods_id"] = $order_id;	//订单ID
		$imgdata["updatetime"] = date("Y-m-d H:i:s");
		$imgdata["createUid"] = 0;
		$imgdata["types"] = 3;
		$imgdata["content"] = '同步平台,物流名称'.$shipping_name.'物流单号='.$shipping_number;
		$imgdata["createUser"] ='auto';
		$this->db1->query($this->db1->insert_string('db_goods_log', $imgdata));
		$this->db1->update("db_order_list",array("is_update_platform"=>'1'),"order_id=".$order_id);
}else{
	$log='';
	$log['orderId']=$order_id;
	$log['updateTime']=date('Y-m-d H:i:s');
	$log['shippingName']=$shipping_name;
	$log['shippingNumber']=$shipping_number;
	$log['types']='ebay';
	$log['content']='自动同步物流信息';
	$this->db1->insert("db_onlineshop_log",$log);
	$this->db1->update("db_order_list",array("is_update_platform"=>'1'),"order_id=".$order_id);

}
		
}



}




}





}

















?>